<template>
  <div class="app-container">
    <h4 class="form-header h4">基本信息</h4>
    <el-form ref="form" :model="form" label-width="80px">
      <el-row>
        <el-col :span="8" :offset="2">
          <el-form-item label="用户昵称" prop="nick_name">
            <el-input v-model="form.nick_name" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="8" :offset="2">
          <el-form-item label="登录账号" prop="user_name">
            <el-input  v-model="form.user_name" disabled />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <h4 class="form-header h4">角色信息</h4>
    <el-table v-loading="loading" :row-key="getRowKey" @row-click="clickRow" ref="table" @selection-change="handleSelection" :data="roles.slice((pageNum-1)*pageSize,pageNum*pageSize)">
      <el-table-column label="序号" type="index" align="center">
        <template slot-scope="scope">
          <span>{{(pageNum - 1) * pageSize + scope.$index + 1}}</span>
        </template>
      </el-table-column>
      <el-table-column type="selection" :reserve-selection="true" width="55"></el-table-column>
      <el-table-column label="角色编号" align="center" prop="id" />
      <el-table-column label="角色名称" align="center" prop="name" />
      <el-table-column label="权限字符" align="center" prop="role_key" />
      <el-table-column label="创建时间" align="center" prop="create_time" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.create_time) }}</span>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="pageNum" :limit.sync="pageSize" />

    <div class="dialog-footer">
      <el-button type="primary" :loading="submitting" :disabled="loading" @click="submitForm()">提交</el-button>
      <el-button @click="close()">返回</el-button>
    </div>
  </div>
</template>

<script>
import { infoAdminApi, updateAuthRoleApi } from '@/api/system/admin'
import { selectRoleApi } from '@/api/system/role'

export default {
  name: "AuthRole",
  data() {
    return {
       // 遮罩层
      loading: true,
      submitting: false,
      // 分页信息
      total: 0,
      pageNum: 1,
      pageSize: 10,
      // 选中角色编号
      roleIds:[],
      // 角色信息
      roles: [],
      // 用户信息
      form: { id: undefined, nick_name: undefined, user_name: undefined, roleIds: [] }
    };
  },
  created() {
    this.form.id = this.$route.params?.adminId || ''
    if (!this.form.id) return this.close()
    this.init()
  },
  methods: {
    async init() {
      await this.getRowData()
      await this.getRoleOptions()
      this.loading = false
    },
    async getRowData() {
      try {
        const { info } = await infoAdminApi(this.form.id)
        this.dataToForm(this.form, info, true)
        this.rowData = info
      } catch (error) {
        this.close()
      }
    },
    async getRoleOptions() {
      try {
        const res = await selectRoleApi()
        this.roles = res?.list || []
        this.total = this.roles.length

        this.$nextTick(() => {
          this.roles.forEach((row) => {
            if (this.form.roleIds.includes(row.id)) {
              this.$refs.table.toggleRowSelection(row,true)
            }
          })
        })
      } catch (e) {}
    },
    /** 单击选中行数据 */
    clickRow(row) {
      this.$refs.table.toggleRowSelection(row)
    },
    // 多选框选中数据
    handleSelection(selection) {
      this.roleIds = selection.map((item) => item.id);
    },
    // 保存选中的数据编号
    getRowKey(row) {
      return row.id
    },
    /** 提交按钮 */
    submitForm() {
      if (this.submitting) return
      this.submitting = true
      updateAuthRoleApi({ id: this.form.id, roleIds: this.roleIds.join(",") }).then(() => {
        this.$modal.msgSuccess("授权成功")
        this.close()
      }).catch(() => {
        this.submitting = false
      })
    },
    /** 关闭按钮 */
    close() {
      const obj = { path: "/system/admin" }
      this.$tab.closeOpenPage(obj);
    },
  },
};
</script>
